package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class MyMetaObjecthandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info(metaObject.toString());
        Long loginId = MyContext.getLoginId();
//        metaObject.setValue("createTime", LocalDateTime.now());
//        metaObject.setValue("updateTime", LocalDateTime.now());

//        metaObject.setValue("createUser",loginId);
//        metaObject.setValue("updateUser", loginId);

        this.strictInsertFill(metaObject,"createTime",LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject,"updateTime",LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject,"createUser",Long.class, loginId);
        this.strictInsertFill(metaObject,"updateUser",Long.class, loginId);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info(metaObject.toString());


        Long loginId = MyContext.getLoginId();

//        metaObject.setValue("updateTime", LocalDateTime.now());
//        metaObject.setValue("updateUser", loginId);

        this.strictInsertFill(metaObject,"updateTime",LocalDateTime.class, LocalDateTime.now());


        this.strictInsertFill(metaObject,"updateUser",Long.class, loginId);
    }
}
